﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Poly2Tri
{
	// Token: 0x020003FC RID: 1020
	public class SplitComplexPolygonNode
	{
		// Token: 0x06001C35 RID: 7221 RVA: 0x000B179A File Offset: 0x000AF99A
		public SplitComplexPolygonNode()
		{
		}

		// Token: 0x06001C36 RID: 7222 RVA: 0x000B17AD File Offset: 0x000AF9AD
		public SplitComplexPolygonNode(Point2D pos)
		{
			this.mPosition = pos;
		}

		// Token: 0x170003FD RID: 1021
		// (get) Token: 0x06001C37 RID: 7223 RVA: 0x000B17C7 File Offset: 0x000AF9C7
		public int NumConnected
		{
			get
			{
				return this.mConnected.Count;
			}
		}

		// Token: 0x170003FE RID: 1022
		// (get) Token: 0x06001C38 RID: 7224 RVA: 0x000B17D4 File Offset: 0x000AF9D4
		// (set) Token: 0x06001C39 RID: 7225 RVA: 0x000B17DC File Offset: 0x000AF9DC
		public Point2D Position
		{
			get
			{
				return this.mPosition;
			}
			set
			{
				this.mPosition = value;
			}
		}

		// Token: 0x170003FF RID: 1023
		public SplitComplexPolygonNode this[int index]
		{
			get
			{
				return this.mConnected[index];
			}
		}

		// Token: 0x06001C3B RID: 7227 RVA: 0x000B17F4 File Offset: 0x000AF9F4
		public override bool Equals(object obj)
		{
			SplitComplexPolygonNode splitComplexPolygonNode = obj as SplitComplexPolygonNode;
			if (splitComplexPolygonNode == null)
			{
				return base.Equals(obj);
			}
			return this.Equals(splitComplexPolygonNode);
		}

		// Token: 0x06001C3C RID: 7228 RVA: 0x000B1823 File Offset: 0x000AFA23
		public bool Equals(SplitComplexPolygonNode pn)
		{
			return pn != null && this.mPosition != null && pn.Position != null && this.mPosition.Equals(pn.Position);
		}

		// Token: 0x06001C3D RID: 7229 RVA: 0x000B1856 File Offset: 0x000AFA56
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		// Token: 0x06001C3E RID: 7230 RVA: 0x000B185E File Offset: 0x000AFA5E
		public static bool operator ==(SplitComplexPolygonNode lhs, SplitComplexPolygonNode rhs)
		{
			if (lhs != null)
			{
				return lhs.Equals(rhs);
			}
			return rhs == null;
		}

		// Token: 0x06001C3F RID: 7231 RVA: 0x000B1877 File Offset: 0x000AFA77
		public static bool operator !=(SplitComplexPolygonNode lhs, SplitComplexPolygonNode rhs)
		{
			if (lhs != null)
			{
				return !lhs.Equals(rhs);
			}
			return rhs != null;
		}

		// Token: 0x06001C40 RID: 7232 RVA: 0x000B1894 File Offset: 0x000AFA94
		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder(256);
			stringBuilder.Append(this.mPosition.ToString());
			stringBuilder.Append(" -> ");
			for (int i = 0; i < this.NumConnected; i++)
			{
				if (i != 0)
				{
					stringBuilder.Append(", ");
				}
				stringBuilder.Append(this.mConnected[i].Position.ToString());
			}
			return stringBuilder.ToString();
		}

		// Token: 0x06001C41 RID: 7233 RVA: 0x000B1918 File Offset: 0x000AFB18
		private bool IsRighter(double sinA, double cosA, double sinB, double cosB)
		{
			if (sinA < 0.0)
			{
				return sinB > 0.0 || cosA <= cosB;
			}
			return sinB >= 0.0 && cosA > cosB;
		}

		// Token: 0x06001C42 RID: 7234 RVA: 0x000B196C File Offset: 0x000AFB6C
		private int remainder(int x, int modulus)
		{
			int i;
			for (i = x % modulus; i < 0; i += modulus)
			{
			}
			return i;
		}

		// Token: 0x06001C43 RID: 7235 RVA: 0x000B198E File Offset: 0x000AFB8E
		public void AddConnection(SplitComplexPolygonNode toMe)
		{
			if (!this.mConnected.Contains(toMe) && toMe != this)
			{
				this.mConnected.Add(toMe);
			}
		}

		// Token: 0x06001C44 RID: 7236 RVA: 0x000B19B9 File Offset: 0x000AFBB9
		public void RemoveConnection(SplitComplexPolygonNode fromMe)
		{
			this.mConnected.Remove(fromMe);
		}

		// Token: 0x06001C45 RID: 7237 RVA: 0x000B19C8 File Offset: 0x000AFBC8
		private void RemoveConnectionByIndex(int index)
		{
			if (index < 0 || index >= this.mConnected.Count)
			{
				return;
			}
			this.mConnected.RemoveAt(index);
		}

		// Token: 0x06001C46 RID: 7238 RVA: 0x000B19EF File Offset: 0x000AFBEF
		public void ClearConnections()
		{
			this.mConnected.Clear();
		}

		// Token: 0x06001C47 RID: 7239 RVA: 0x000B19FC File Offset: 0x000AFBFC
		private bool IsConnectedTo(SplitComplexPolygonNode me)
		{
			return this.mConnected.Contains(me);
		}

		// Token: 0x06001C48 RID: 7240 RVA: 0x000B1A0C File Offset: 0x000AFC0C
		public SplitComplexPolygonNode GetRightestConnection(SplitComplexPolygonNode incoming)
		{
			if (this.NumConnected == 0)
			{
				throw new Exception("the connection graph is inconsistent");
			}
			if (this.NumConnected == 1)
			{
				return incoming;
			}
			Point2D point2D = this.mPosition - incoming.mPosition;
			double num = point2D.Magnitude();
			point2D.Normalize();
			if (num <= MathUtil.EPSILON)
			{
				throw new Exception("Length too small");
			}
			SplitComplexPolygonNode splitComplexPolygonNode = null;
			for (int i = 0; i < this.NumConnected; i++)
			{
				if (!(this.mConnected[i] == incoming))
				{
					Point2D point2D2 = this.mConnected[i].mPosition - this.mPosition;
					double num2 = point2D2.MagnitudeSquared();
					point2D2.Normalize();
					if (num2 <= MathUtil.EPSILON * MathUtil.EPSILON)
					{
						throw new Exception("Length too small");
					}
					double cosA = Point2D.Dot(point2D, point2D2);
					double sinA = Point2D.Cross(point2D, point2D2);
					if (splitComplexPolygonNode != null)
					{
						Point2D point2D3 = splitComplexPolygonNode.mPosition - this.mPosition;
						point2D3.Normalize();
						double cosB = Point2D.Dot(point2D, point2D3);
						double sinB = Point2D.Cross(point2D, point2D3);
						if (this.IsRighter(sinA, cosA, sinB, cosB))
						{
							splitComplexPolygonNode = this.mConnected[i];
						}
					}
					else
					{
						splitComplexPolygonNode = this.mConnected[i];
					}
				}
			}
			return splitComplexPolygonNode;
		}

		// Token: 0x06001C49 RID: 7241 RVA: 0x000B1B74 File Offset: 0x000AFD74
		public SplitComplexPolygonNode GetRightestConnection(Point2D incomingDir)
		{
			Point2D pos = this.mPosition - incomingDir;
			SplitComplexPolygonNode incoming = new SplitComplexPolygonNode(pos);
			return this.GetRightestConnection(incoming);
		}

		// Token: 0x04001D81 RID: 7553
		private List<SplitComplexPolygonNode> mConnected = new List<SplitComplexPolygonNode>();

		// Token: 0x04001D82 RID: 7554
		private Point2D mPosition;
	}
}
